{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This Python 3 environment comes with many helpful analytics libraries installed\n",
    "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
    "# For example, here's several helpful packages to load in \n",
    "\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "from joblib import dump, load\n",
    "from sklearn.metrics import accuracy_score, f1_score, precision_score,recall_score\n",
    "from sklearn.linear_model import Perceptron\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier, VotingClassifier\n",
    "from sklearn.ensemble import BaggingClassifier\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn import svm, datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.utils.multiclass import unique_labels\n",
    "from sklearn.metrics import roc_curve, auc\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import label_binarize\n",
    "from sklearn.multiclass import OneVsRestClassifier\n",
    "from scipy import interp\n",
    "from itertools import cycle\n",
    "import seaborn as sns\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import roc_curve\n",
    "from sklearn.metrics import roc_auc_score\n",
    "import sklearn.metrics as metrics\n",
    "\n",
    "# Input data files are available in the \"../input/\" directory.\n",
    "# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
    "\n",
    "import os\n",
    "for dirname, _, filenames in os.walk('/kaggle/input'):\n",
    "    for filename in filenames:\n",
    "        print(os.path.join(dirname, filename))\n",
    "\n",
    "# Any results you write to the current directory are saved as output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.0\n",
      "1.19.5\n",
      "3.8.6 | packaged by conda-forge | (default, Dec 26 2020, 04:50:20) \n",
      "[Clang 11.0.0 ]\n",
      "0.24.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "import keras\n",
    "import sklearn\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Activation, Embedding\n",
    "from keras.layers import LSTM, SimpleRNN, GRU, Bidirectional, BatchNormalization,Convolution1D,MaxPooling1D, Reshape, GlobalAveragePooling1D\n",
    "from tensorflow.keras.utils import to_categorical\n",
    "import sklearn.preprocessing\n",
    "from sklearn import metrics\n",
    "from scipy.stats import zscore\n",
    "from tensorflow.keras.utils import get_file, plot_model\n",
    "from sklearn.model_selection import train_test_split\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "import matplotlib.pyplot as plt\n",
    "print(pd.__version__)\n",
    "print(np.__version__)\n",
    "print(sys.version)\n",
    "print(sklearn.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>33</th>\n",
       "      <th>34</th>\n",
       "      <th>35</th>\n",
       "      <th>36</th>\n",
       "      <th>37</th>\n",
       "      <th>38</th>\n",
       "      <th>39</th>\n",
       "      <th>40</th>\n",
       "      <th>41</th>\n",
       "      <th>42</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp_data</td>\n",
       "      <td>SF</td>\n",
       "      <td>491</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>udp</td>\n",
       "      <td>other</td>\n",
       "      <td>SF</td>\n",
       "      <td>146</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>S0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>232</td>\n",
       "      <td>8153</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.01</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>199</td>\n",
       "      <td>420</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   0    1         2   3    4     5   6   7   8   9   ...    33    34    35  \\\n",
       "0   0  tcp  ftp_data  SF  491     0   0   0   0   0  ...  0.17  0.03  0.17   \n",
       "1   0  udp     other  SF  146     0   0   0   0   0  ...  0.00  0.60  0.88   \n",
       "2   0  tcp   private  S0    0     0   0   0   0   0  ...  0.10  0.05  0.00   \n",
       "3   0  tcp      http  SF  232  8153   0   0   0   0  ...  1.00  0.00  0.03   \n",
       "4   0  tcp      http  SF  199   420   0   0   0   0  ...  1.00  0.00  0.00   \n",
       "\n",
       "     36    37    38    39    40       41  42  \n",
       "0  0.00  0.00  0.00  0.05  0.00   normal  20  \n",
       "1  0.00  0.00  0.00  0.00  0.00   normal  15  \n",
       "2  0.00  1.00  1.00  0.00  0.00  neptune  19  \n",
       "3  0.04  0.03  0.01  0.00  0.01   normal  21  \n",
       "4  0.00  0.00  0.00  0.00  0.00   normal  21  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Loading training set into dataframe\n",
    "df = pd.read_csv('KDDTrain+.txt', header=None)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>33</th>\n",
       "      <th>34</th>\n",
       "      <th>35</th>\n",
       "      <th>36</th>\n",
       "      <th>37</th>\n",
       "      <th>38</th>\n",
       "      <th>39</th>\n",
       "      <th>40</th>\n",
       "      <th>41</th>\n",
       "      <th>42</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>REJ</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>REJ</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp_data</td>\n",
       "      <td>SF</td>\n",
       "      <td>12983</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>eco_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.28</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>saint</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>tcp</td>\n",
       "      <td>telnet</td>\n",
       "      <td>RSTO</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.31</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.83</td>\n",
       "      <td>0.71</td>\n",
       "      <td>mscan</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   0     1         2     3      4   5   6   7   8   9   ...    33    34    35  \\\n",
       "0   0   tcp   private   REJ      0   0   0   0   0   0  ...  0.04  0.06  0.00   \n",
       "1   0   tcp   private   REJ      0   0   0   0   0   0  ...  0.00  0.06  0.00   \n",
       "2   2   tcp  ftp_data    SF  12983   0   0   0   0   0  ...  0.61  0.04  0.61   \n",
       "3   0  icmp     eco_i    SF     20   0   0   0   0   0  ...  1.00  0.00  1.00   \n",
       "4   1   tcp    telnet  RSTO      0  15   0   0   0   0  ...  0.31  0.17  0.03   \n",
       "\n",
       "     36   37   38    39    40       41  42  \n",
       "0  0.00  0.0  0.0  1.00  1.00  neptune  21  \n",
       "1  0.00  0.0  0.0  1.00  1.00  neptune  21  \n",
       "2  0.02  0.0  0.0  0.00  0.00   normal  21  \n",
       "3  0.28  0.0  0.0  0.00  0.00    saint  15  \n",
       "4  0.02  0.0  0.0  0.83  0.71    mscan  11  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Loading testing set into dataframe\n",
    "qp = pd.read_csv('KDDTest+.txt', header=None)\n",
    "qp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>protocol_type</th>\n",
       "      <th>service</th>\n",
       "      <th>flag</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>...</th>\n",
       "      <th>dst_host_same_srv_rate</th>\n",
       "      <th>dst_host_diff_srv_rate</th>\n",
       "      <th>dst_host_same_src_port_rate</th>\n",
       "      <th>dst_host_srv_diff_host_rate</th>\n",
       "      <th>dst_host_serror_rate</th>\n",
       "      <th>dst_host_srv_serror_rate</th>\n",
       "      <th>dst_host_rerror_rate</th>\n",
       "      <th>dst_host_srv_rerror_rate</th>\n",
       "      <th>subclass</th>\n",
       "      <th>difficulty_level</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp_data</td>\n",
       "      <td>SF</td>\n",
       "      <td>491</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>udp</td>\n",
       "      <td>other</td>\n",
       "      <td>SF</td>\n",
       "      <td>146</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>S0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>232</td>\n",
       "      <td>8153</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.01</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>199</td>\n",
       "      <td>420</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   duration protocol_type   service flag  src_bytes  dst_bytes  land  \\\n",
       "0         0           tcp  ftp_data   SF        491          0     0   \n",
       "1         0           udp     other   SF        146          0     0   \n",
       "2         0           tcp   private   S0          0          0     0   \n",
       "3         0           tcp      http   SF        232       8153     0   \n",
       "4         0           tcp      http   SF        199        420     0   \n",
       "\n",
       "   wrong_fragment  urgent  hot  ...  dst_host_same_srv_rate  \\\n",
       "0               0       0    0  ...                    0.17   \n",
       "1               0       0    0  ...                    0.00   \n",
       "2               0       0    0  ...                    0.10   \n",
       "3               0       0    0  ...                    1.00   \n",
       "4               0       0    0  ...                    1.00   \n",
       "\n",
       "   dst_host_diff_srv_rate  dst_host_same_src_port_rate  \\\n",
       "0                    0.03                         0.17   \n",
       "1                    0.60                         0.88   \n",
       "2                    0.05                         0.00   \n",
       "3                    0.00                         0.03   \n",
       "4                    0.00                         0.00   \n",
       "\n",
       "   dst_host_srv_diff_host_rate  dst_host_serror_rate  \\\n",
       "0                         0.00                  0.00   \n",
       "1                         0.00                  0.00   \n",
       "2                         0.00                  1.00   \n",
       "3                         0.04                  0.03   \n",
       "4                         0.00                  0.00   \n",
       "\n",
       "   dst_host_srv_serror_rate  dst_host_rerror_rate  dst_host_srv_rerror_rate  \\\n",
       "0                      0.00                  0.05                      0.00   \n",
       "1                      0.00                  0.00                      0.00   \n",
       "2                      1.00                  0.00                      0.00   \n",
       "3                      0.01                  0.00                      0.01   \n",
       "4                      0.00                  0.00                      0.00   \n",
       "\n",
       "   subclass  difficulty_level  \n",
       "0    normal                20  \n",
       "1    normal                15  \n",
       "2   neptune                19  \n",
       "3    normal                21  \n",
       "4    normal                21  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Reset column names for training set\n",
    "df.columns = ['duration', 'protocol_type', 'service', 'flag', 'src_bytes',\n",
    "'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot',\n",
    "'num_failed_logins', 'logged_in', 'num_compromised', 'root_shell',\n",
    "'su_attempted', 'num_root', 'num_file_creations', 'num_shells',\n",
    "'num_access_files', 'num_outbound_cmds', 'is_host_login',\n",
    "'is_guest_login', 'count', 'srv_count', 'serror_rate',\n",
    "'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate',\n",
    "'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',\n",
    "'dst_host_srv_count', 'dst_host_same_srv_rate','dst_host_diff_srv_rate', 'dst_host_same_src_port_rate',\n",
    "'dst_host_srv_diff_host_rate', 'dst_host_serror_rate',\n",
    "'dst_host_srv_serror_rate', 'dst_host_rerror_rate',\n",
    "'dst_host_srv_rerror_rate', 'subclass', 'difficulty_level']\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>protocol_type</th>\n",
       "      <th>service</th>\n",
       "      <th>flag</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>...</th>\n",
       "      <th>dst_host_same_srv_rate</th>\n",
       "      <th>dst_host_diff_srv_rate</th>\n",
       "      <th>dst_host_same_src_port_rate</th>\n",
       "      <th>dst_host_srv_diff_host_rate</th>\n",
       "      <th>dst_host_serror_rate</th>\n",
       "      <th>dst_host_srv_serror_rate</th>\n",
       "      <th>dst_host_rerror_rate</th>\n",
       "      <th>dst_host_srv_rerror_rate</th>\n",
       "      <th>subclass</th>\n",
       "      <th>difficulty_level</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>REJ</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>private</td>\n",
       "      <td>REJ</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>neptune</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp_data</td>\n",
       "      <td>SF</td>\n",
       "      <td>12983</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>normal</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>eco_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.28</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>saint</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>tcp</td>\n",
       "      <td>telnet</td>\n",
       "      <td>RSTO</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.31</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.83</td>\n",
       "      <td>0.71</td>\n",
       "      <td>mscan</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   duration protocol_type   service  flag  src_bytes  dst_bytes  land  \\\n",
       "0         0           tcp   private   REJ          0          0     0   \n",
       "1         0           tcp   private   REJ          0          0     0   \n",
       "2         2           tcp  ftp_data    SF      12983          0     0   \n",
       "3         0          icmp     eco_i    SF         20          0     0   \n",
       "4         1           tcp    telnet  RSTO          0         15     0   \n",
       "\n",
       "   wrong_fragment  urgent  hot  ...  dst_host_same_srv_rate  \\\n",
       "0               0       0    0  ...                    0.04   \n",
       "1               0       0    0  ...                    0.00   \n",
       "2               0       0    0  ...                    0.61   \n",
       "3               0       0    0  ...                    1.00   \n",
       "4               0       0    0  ...                    0.31   \n",
       "\n",
       "   dst_host_diff_srv_rate  dst_host_same_src_port_rate  \\\n",
       "0                    0.06                         0.00   \n",
       "1                    0.06                         0.00   \n",
       "2                    0.04                         0.61   \n",
       "3                    0.00                         1.00   \n",
       "4                    0.17                         0.03   \n",
       "\n",
       "   dst_host_srv_diff_host_rate  dst_host_serror_rate  \\\n",
       "0                         0.00                   0.0   \n",
       "1                         0.00                   0.0   \n",
       "2                         0.02                   0.0   \n",
       "3                         0.28                   0.0   \n",
       "4                         0.02                   0.0   \n",
       "\n",
       "   dst_host_srv_serror_rate  dst_host_rerror_rate  dst_host_srv_rerror_rate  \\\n",
       "0                       0.0                  1.00                      1.00   \n",
       "1                       0.0                  1.00                      1.00   \n",
       "2                       0.0                  0.00                      0.00   \n",
       "3                       0.0                  0.00                      0.00   \n",
       "4                       0.0                  0.83                      0.71   \n",
       "\n",
       "   subclass  difficulty_level  \n",
       "0   neptune                21  \n",
       "1   neptune                21  \n",
       "2    normal                21  \n",
       "3     saint                15  \n",
       "4     mscan                11  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Reset column names for testing set\n",
    "qp.columns = ['duration', 'protocol_type', 'service', 'flag', 'src_bytes',\n",
    "'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot',\n",
    "'num_failed_logins', 'logged_in', 'num_compromised', 'root_shell',\n",
    "'su_attempted', 'num_root', 'num_file_creations', 'num_shells',\n",
    "'num_access_files', 'num_outbound_cmds', 'is_host_login',\n",
    "'is_guest_login', 'count', 'srv_count', 'serror_rate',\n",
    "'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate',\n",
    "'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',\n",
    "'dst_host_srv_count', 'dst_host_same_srv_rate','dst_host_diff_srv_rate', 'dst_host_same_src_port_rate',\n",
    "'dst_host_srv_diff_host_rate', 'dst_host_serror_rate',\n",
    "'dst_host_srv_serror_rate', 'dst_host_rerror_rate',\n",
    "'dst_host_srv_rerror_rate', 'subclass', 'difficulty_level']\n",
    "qp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['duration', 'protocol_type', 'service', 'flag', 'src_bytes',\n",
       "       'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot',\n",
       "       'num_failed_logins', 'logged_in', 'num_compromised', 'root_shell',\n",
       "       'su_attempted', 'num_root', 'num_file_creations', 'num_shells',\n",
       "       'num_access_files', 'num_outbound_cmds', 'is_host_login',\n",
       "       'is_guest_login', 'count', 'srv_count', 'serror_rate',\n",
       "       'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate',\n",
       "       'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',\n",
       "       'dst_host_srv_count', 'dst_host_same_srv_rate',\n",
       "       'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate',\n",
       "       'dst_host_srv_diff_host_rate', 'dst_host_serror_rate',\n",
       "       'dst_host_srv_serror_rate', 'dst_host_rerror_rate',\n",
       "       'dst_host_srv_rerror_rate', 'subclass', 'difficulty_level'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#accessing names of training columns\n",
    "lst_names = df.columns # returns a list of column names\n",
    "lst_names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['duration', 'protocol_type', 'service', 'flag', 'src_bytes',\n",
       "       'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot',\n",
       "       'num_failed_logins', 'logged_in', 'num_compromised', 'root_shell',\n",
       "       'su_attempted', 'num_root', 'num_file_creations', 'num_shells',\n",
       "       'num_access_files', 'num_outbound_cmds', 'is_host_login',\n",
       "       'is_guest_login', 'count', 'srv_count', 'serror_rate',\n",
       "       'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate',\n",
       "       'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',\n",
       "       'dst_host_srv_count', 'dst_host_same_srv_rate',\n",
       "       'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate',\n",
       "       'dst_host_srv_diff_host_rate', 'dst_host_serror_rate',\n",
       "       'dst_host_srv_serror_rate', 'dst_host_rerror_rate',\n",
       "       'dst_host_srv_rerror_rate', 'subclass', 'difficulty_level'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#accessing names of testing columns\n",
    "testlst_names = qp.columns\n",
    "testlst_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(125973, 42)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Dropping the last columns of training set\n",
    "df = df.drop('difficulty_level', 1) # we don't need it in this project\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(22544, 42)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Dropping the last columns of testing set\n",
    "qp = qp.drop('difficulty_level', 1)\n",
    "qp.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qp.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['protocol_type', 'service', 'flag']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#defining col list\n",
    "cols = ['protocol_type','service','flag']\n",
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#One-hot encoding\n",
    "def one_hot(df, cols):\n",
    "    \"\"\"\n",
    "    @param df pandas DataFrame\n",
    "    @param cols a list of columns to encode\n",
    "    @return a DataFrame with one-hot encoding\n",
    "    \"\"\"\n",
    "    for each in cols:\n",
    "        dummies = pd.get_dummies(df[each], prefix=each, drop_first=False)\n",
    "        df = pd.concat([df, dummies], axis=1)\n",
    "        df = df.drop(each, 1)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Merging train and test data\n",
    "combined_data = pd.concat([df,qp])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Applying one hot encoding to combined data\n",
    "combined_data = one_hot(combined_data,cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Function to min-max normalize\n",
    "def normalize(df, cols):\n",
    "    \"\"\"\n",
    "    @param df pandas DataFrame\n",
    "    @param cols a list of columns to encode\n",
    "    @return a DataFrame with normalized specified features\n",
    "    \"\"\"\n",
    "    result = df.copy() # do not touch the original df\n",
    "    for feature_name in cols:\n",
    "        max_value = df[feature_name].max()\n",
    "        min_value = df[feature_name].min()\n",
    "        if max_value > min_value:\n",
    "            result[feature_name] = (df[feature_name] - min_value) / (max_value - min_value)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Dropping subclass column for training set\n",
    "tmp = combined_data.pop('subclass')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>num_failed_logins</th>\n",
       "      <th>logged_in</th>\n",
       "      <th>num_compromised</th>\n",
       "      <th>...</th>\n",
       "      <th>flag_REJ</th>\n",
       "      <th>flag_RSTO</th>\n",
       "      <th>flag_RSTOS0</th>\n",
       "      <th>flag_RSTR</th>\n",
       "      <th>flag_S0</th>\n",
       "      <th>flag_S1</th>\n",
       "      <th>flag_S2</th>\n",
       "      <th>flag_S3</th>\n",
       "      <th>flag_SF</th>\n",
       "      <th>flag_SH</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.558064e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.057999e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.681203e-07</td>\n",
       "      <td>6.223962e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.442067e-07</td>\n",
       "      <td>3.206260e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22539</th>\n",
       "      <td>0.0</td>\n",
       "      <td>5.753774e-07</td>\n",
       "      <td>2.542106e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22540</th>\n",
       "      <td>0.0</td>\n",
       "      <td>2.297162e-07</td>\n",
       "      <td>7.160648e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22541</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.952277e-05</td>\n",
       "      <td>6.346868e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.019802</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000134</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22542</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.043558e-08</td>\n",
       "      <td>3.206260e-08</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22543</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>148517 rows × 122 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       duration     src_bytes     dst_bytes  land  wrong_fragment  urgent  \\\n",
       "0           0.0  3.558064e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "1           0.0  1.057999e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "2           0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "3           0.0  1.681203e-07  6.223962e-06   0.0             0.0     0.0   \n",
       "4           0.0  1.442067e-07  3.206260e-07   0.0             0.0     0.0   \n",
       "...         ...           ...           ...   ...             ...     ...   \n",
       "22539       0.0  5.753774e-07  2.542106e-07   0.0             0.0     0.0   \n",
       "22540       0.0  2.297162e-07  7.160648e-07   0.0             0.0     0.0   \n",
       "22541       0.0  3.952277e-05  6.346868e-06   0.0             0.0     0.0   \n",
       "22542       0.0  3.043558e-08  3.206260e-08   0.0             0.0     0.0   \n",
       "22543       0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "\n",
       "            hot  num_failed_logins  logged_in  num_compromised  ...  flag_REJ  \\\n",
       "0      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "1      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "2      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "3      0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "4      0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "...         ...                ...        ...              ...  ...       ...   \n",
       "22539  0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "22540  0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "22541  0.019802                0.0        1.0         0.000134  ...       0.0   \n",
       "22542  0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "22543  0.000000                0.0        0.0         0.000000  ...       1.0   \n",
       "\n",
       "       flag_RSTO  flag_RSTOS0  flag_RSTR  flag_S0  flag_S1  flag_S2  flag_S3  \\\n",
       "0            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "1            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "2            0.0          0.0        0.0      1.0      0.0      0.0      0.0   \n",
       "3            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "4            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "...          ...          ...        ...      ...      ...      ...      ...   \n",
       "22539        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22540        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22541        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22542        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22543        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "\n",
       "       flag_SF  flag_SH  \n",
       "0          1.0      0.0  \n",
       "1          1.0      0.0  \n",
       "2          0.0      0.0  \n",
       "3          1.0      0.0  \n",
       "4          1.0      0.0  \n",
       "...        ...      ...  \n",
       "22539      1.0      0.0  \n",
       "22540      1.0      0.0  \n",
       "22541      1.0      0.0  \n",
       "22542      1.0      0.0  \n",
       "22543      0.0      0.0  \n",
       "\n",
       "[148517 rows x 122 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Normalizing training set\n",
    "new_train_df = normalize(combined_data,combined_data.columns)\n",
    "new_train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Fixing labels for training set\n",
    "classlist = []\n",
    "check1 = (\"apache2\",\"back\",\"land\",\"neptune\",\"mailbomb\",\"pod\",\"processtable\",\"smurf\",\"teardrop\",\"udpstorm\",\"worm\")\n",
    "check2 = (\"ipsweep\",\"mscan\",\"nmap\",\"portsweep\",\"saint\",\"satan\")\n",
    "check3 = (\"buffer_overflow\",\"loadmodule\",\"perl\",\"ps\",\"rootkit\",\"sqlattack\",\"xterm\")\n",
    "check4 = (\"ftp_write\",\"guess_passwd\",\"httptunnel\",\"imap\",\"multihop\",\"named\",\"phf\",\"sendmail\",\"Snmpgetattack\",\"spy\",\"snmpguess\",\"warezclient\",\"warezmaster\",\"xlock\",\"xsnoop\")\n",
    "\n",
    "DoSCount=0\n",
    "ProbeCount=0\n",
    "U2RCount=0\n",
    "R2LCount=0\n",
    "NormalCount=0\n",
    "\n",
    "for item in tmp:\n",
    "    if item in check1:\n",
    "        classlist.append(\"DoS\")\n",
    "        DoSCount=DoSCount+1\n",
    "    elif item in check2:\n",
    "        classlist.append(\"Probe\")\n",
    "        ProbeCount=ProbeCount+1\n",
    "    elif item in check3:\n",
    "        classlist.append(\"U2R\")\n",
    "        U2RCount=U2RCount+1\n",
    "    elif item in check4:\n",
    "        classlist.append(\"R2L\")\n",
    "        R2LCount=R2LCount+1\n",
    "    else:\n",
    "        classlist.append(\"Normal\")\n",
    "        NormalCount=NormalCount+1   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "53387"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DoSCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>num_failed_logins</th>\n",
       "      <th>logged_in</th>\n",
       "      <th>num_compromised</th>\n",
       "      <th>...</th>\n",
       "      <th>flag_RSTO</th>\n",
       "      <th>flag_RSTOS0</th>\n",
       "      <th>flag_RSTR</th>\n",
       "      <th>flag_S0</th>\n",
       "      <th>flag_S1</th>\n",
       "      <th>flag_S2</th>\n",
       "      <th>flag_S3</th>\n",
       "      <th>flag_SF</th>\n",
       "      <th>flag_SH</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.558064e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.057999e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>DoS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.681203e-07</td>\n",
       "      <td>6.223962e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.442067e-07</td>\n",
       "      <td>3.206260e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22539</th>\n",
       "      <td>0.0</td>\n",
       "      <td>5.753774e-07</td>\n",
       "      <td>2.542106e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22540</th>\n",
       "      <td>0.0</td>\n",
       "      <td>2.297162e-07</td>\n",
       "      <td>7.160648e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22541</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.952277e-05</td>\n",
       "      <td>6.346868e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.019802</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000134</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>DoS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22542</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.043558e-08</td>\n",
       "      <td>3.206260e-08</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22543</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Probe</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>148517 rows × 123 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       duration     src_bytes     dst_bytes  land  wrong_fragment  urgent  \\\n",
       "0           0.0  3.558064e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "1           0.0  1.057999e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "2           0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "3           0.0  1.681203e-07  6.223962e-06   0.0             0.0     0.0   \n",
       "4           0.0  1.442067e-07  3.206260e-07   0.0             0.0     0.0   \n",
       "...         ...           ...           ...   ...             ...     ...   \n",
       "22539       0.0  5.753774e-07  2.542106e-07   0.0             0.0     0.0   \n",
       "22540       0.0  2.297162e-07  7.160648e-07   0.0             0.0     0.0   \n",
       "22541       0.0  3.952277e-05  6.346868e-06   0.0             0.0     0.0   \n",
       "22542       0.0  3.043558e-08  3.206260e-08   0.0             0.0     0.0   \n",
       "22543       0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "\n",
       "            hot  num_failed_logins  logged_in  num_compromised  ...  \\\n",
       "0      0.000000                0.0        0.0         0.000000  ...   \n",
       "1      0.000000                0.0        0.0         0.000000  ...   \n",
       "2      0.000000                0.0        0.0         0.000000  ...   \n",
       "3      0.000000                0.0        1.0         0.000000  ...   \n",
       "4      0.000000                0.0        1.0         0.000000  ...   \n",
       "...         ...                ...        ...              ...  ...   \n",
       "22539  0.000000                0.0        1.0         0.000000  ...   \n",
       "22540  0.000000                0.0        1.0         0.000000  ...   \n",
       "22541  0.019802                0.0        1.0         0.000134  ...   \n",
       "22542  0.000000                0.0        0.0         0.000000  ...   \n",
       "22543  0.000000                0.0        0.0         0.000000  ...   \n",
       "\n",
       "       flag_RSTO  flag_RSTOS0  flag_RSTR  flag_S0  flag_S1  flag_S2  flag_S3  \\\n",
       "0            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "1            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "2            0.0          0.0        0.0      1.0      0.0      0.0      0.0   \n",
       "3            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "4            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "...          ...          ...        ...      ...      ...      ...      ...   \n",
       "22539        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22540        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22541        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22542        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22543        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "\n",
       "       flag_SF  flag_SH   Class  \n",
       "0          1.0      0.0  Normal  \n",
       "1          1.0      0.0  Normal  \n",
       "2          0.0      0.0     DoS  \n",
       "3          1.0      0.0  Normal  \n",
       "4          1.0      0.0  Normal  \n",
       "...        ...      ...     ...  \n",
       "22539      1.0      0.0  Normal  \n",
       "22540      1.0      0.0  Normal  \n",
       "22541      1.0      0.0     DoS  \n",
       "22542      1.0      0.0  Normal  \n",
       "22543      0.0      0.0   Probe  \n",
       "\n",
       "[148517 rows x 123 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Appending class column to training set\n",
    "new_train_df[\"Class\"] = classlist\n",
    "new_train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal    77232\n",
       "DoS       53387\n",
       "Probe     14077\n",
       "R2L        3702\n",
       "U2R         119\n",
       "Name: Class, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_train_df[\"Class\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_train_df.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Normal\n",
       "1        Normal\n",
       "2           DoS\n",
       "3        Normal\n",
       "4        Normal\n",
       "          ...  \n",
       "22539    Normal\n",
       "22540    Normal\n",
       "22541       DoS\n",
       "22542    Normal\n",
       "22543     Probe\n",
       "Name: Class, Length: 148517, dtype: object"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train=new_train_df[\"Class\"]\n",
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>num_failed_logins</th>\n",
       "      <th>logged_in</th>\n",
       "      <th>num_compromised</th>\n",
       "      <th>...</th>\n",
       "      <th>flag_REJ</th>\n",
       "      <th>flag_RSTO</th>\n",
       "      <th>flag_RSTOS0</th>\n",
       "      <th>flag_RSTR</th>\n",
       "      <th>flag_S0</th>\n",
       "      <th>flag_S1</th>\n",
       "      <th>flag_S2</th>\n",
       "      <th>flag_S3</th>\n",
       "      <th>flag_SF</th>\n",
       "      <th>flag_SH</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.558064e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.057999e-07</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.681203e-07</td>\n",
       "      <td>6.223962e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.442067e-07</td>\n",
       "      <td>3.206260e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22539</th>\n",
       "      <td>0.0</td>\n",
       "      <td>5.753774e-07</td>\n",
       "      <td>2.542106e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22540</th>\n",
       "      <td>0.0</td>\n",
       "      <td>2.297162e-07</td>\n",
       "      <td>7.160648e-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22541</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.952277e-05</td>\n",
       "      <td>6.346868e-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.019802</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000134</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22542</th>\n",
       "      <td>0.0</td>\n",
       "      <td>3.043558e-08</td>\n",
       "      <td>3.206260e-08</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22543</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>148517 rows × 122 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       duration     src_bytes     dst_bytes  land  wrong_fragment  urgent  \\\n",
       "0           0.0  3.558064e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "1           0.0  1.057999e-07  0.000000e+00   0.0             0.0     0.0   \n",
       "2           0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "3           0.0  1.681203e-07  6.223962e-06   0.0             0.0     0.0   \n",
       "4           0.0  1.442067e-07  3.206260e-07   0.0             0.0     0.0   \n",
       "...         ...           ...           ...   ...             ...     ...   \n",
       "22539       0.0  5.753774e-07  2.542106e-07   0.0             0.0     0.0   \n",
       "22540       0.0  2.297162e-07  7.160648e-07   0.0             0.0     0.0   \n",
       "22541       0.0  3.952277e-05  6.346868e-06   0.0             0.0     0.0   \n",
       "22542       0.0  3.043558e-08  3.206260e-08   0.0             0.0     0.0   \n",
       "22543       0.0  0.000000e+00  0.000000e+00   0.0             0.0     0.0   \n",
       "\n",
       "            hot  num_failed_logins  logged_in  num_compromised  ...  flag_REJ  \\\n",
       "0      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "1      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "2      0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "3      0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "4      0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "...         ...                ...        ...              ...  ...       ...   \n",
       "22539  0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "22540  0.000000                0.0        1.0         0.000000  ...       0.0   \n",
       "22541  0.019802                0.0        1.0         0.000134  ...       0.0   \n",
       "22542  0.000000                0.0        0.0         0.000000  ...       0.0   \n",
       "22543  0.000000                0.0        0.0         0.000000  ...       1.0   \n",
       "\n",
       "       flag_RSTO  flag_RSTOS0  flag_RSTR  flag_S0  flag_S1  flag_S2  flag_S3  \\\n",
       "0            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "1            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "2            0.0          0.0        0.0      1.0      0.0      0.0      0.0   \n",
       "3            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "4            0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "...          ...          ...        ...      ...      ...      ...      ...   \n",
       "22539        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22540        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22541        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22542        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "22543        0.0          0.0        0.0      0.0      0.0      0.0      0.0   \n",
       "\n",
       "       flag_SF  flag_SH  \n",
       "0          1.0      0.0  \n",
       "1          1.0      0.0  \n",
       "2          0.0      0.0  \n",
       "3          1.0      0.0  \n",
       "4          1.0      0.0  \n",
       "...        ...      ...  \n",
       "22539      1.0      0.0  \n",
       "22540      1.0      0.0  \n",
       "22541      1.0      0.0  \n",
       "22542      1.0      0.0  \n",
       "22543      0.0      0.0  \n",
       "\n",
       "[148517 rows x 122 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combined_data_X = new_train_df.drop('Class', 1)\n",
    "combined_data_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "oos_pred = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import StratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kfold = StratifiedKFold(n_splits=6,shuffle=True,random_state=42)\n",
    "kfold.get_n_splits(combined_data_X,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "    #Bidirectional RNN\n",
    "    batch_size = 32\n",
    "    model = Sequential()\n",
    "    model.add(Convolution1D(64, kernel_size=122, padding=\"same\",activation=\"relu\",input_shape=(122, 1)))\n",
    "    model.add(MaxPooling1D(pool_size=(5)))\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(Bidirectional(LSTM(64, return_sequences=False))) \n",
    "    model.add(Reshape((128, 1), input_shape = (128, )))\n",
    "    \n",
    "    model.add(MaxPooling1D(pool_size=(5)))\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(Bidirectional(LSTM(128, return_sequences=False))) \n",
    "    \n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(5))\n",
    "    model.add(Activation('softmax'))\n",
    "    model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 122, 64)\n",
      "(None, 24, 64)\n",
      "(None, 24, 64)\n",
      "(None, 128)\n",
      "(None, 128, 1)\n",
      "(None, 25, 1)\n",
      "(None, 25, 1)\n",
      "(None, 256)\n",
      "(None, 256)\n",
      "(None, 5)\n",
      "(None, 5)\n"
     ]
    }
   ],
   "source": [
    "for layer in model.layers:\n",
    "    print(layer.output_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_5\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv1d_3 (Conv1D)            (None, 122, 64)           7872      \n",
      "_________________________________________________________________\n",
      "max_pooling1d_3 (MaxPooling1 (None, 24, 64)            0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_3 (Batch (None, 24, 64)            256       \n",
      "_________________________________________________________________\n",
      "bidirectional_3 (Bidirection (None, 128)               66048     \n",
      "_________________________________________________________________\n",
      "reshape_2 (Reshape)          (None, 128, 1)            0         \n",
      "_________________________________________________________________\n",
      "max_pooling1d_4 (MaxPooling1 (None, 25, 1)             0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_4 (Batch (None, 25, 1)             4         \n",
      "_________________________________________________________________\n",
      "bidirectional_4 (Bidirection (None, 256)               133120    \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 256)               0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 5)                 1285      \n",
      "_________________________________________________________________\n",
      "activation_1 (Activation)    (None, 5)                 0         \n",
      "=================================================================\n",
      "Total params: 208,585\n",
      "Trainable params: 208,455\n",
      "Non-trainable params: 130\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index: [     0      2      3 ... 148514 148515 148516]\n",
      "test index: [     1      7     15 ... 148506 148511 148513]\n",
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 252s 63ms/step - loss: 0.1084 - accuracy: 0.9661 - val_loss: 0.0880 - val_accuracy: 0.9718\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 248s 64ms/step - loss: 0.0670 - accuracy: 0.9774 - val_loss: 0.0647 - val_accuracy: 0.9805\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 242s 62ms/step - loss: 0.0532 - accuracy: 0.9818 - val_loss: 0.0463 - val_accuracy: 0.9819\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 338s 87ms/step - loss: 0.0448 - accuracy: 0.9848 - val_loss: 0.0354 - val_accuracy: 0.9892\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 243s 63ms/step - loss: 0.0386 - accuracy: 0.9867 - val_loss: 0.0336 - val_accuracy: 0.9876\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 151s 39ms/step - loss: 0.0346 - accuracy: 0.9879 - val_loss: 0.0383 - val_accuracy: 0.9875\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 251s 65ms/step - loss: 0.0336 - accuracy: 0.9883 - val_loss: 0.0329 - val_accuracy: 0.9890\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 224s 58ms/step - loss: 0.0315 - accuracy: 0.9891 - val_loss: 0.0322 - val_accuracy: 0.9888\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 224s 58ms/step - loss: 0.0291 - accuracy: 0.9899 - val_loss: 0.0351 - val_accuracy: 0.9888\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 251s 65ms/step - loss: 0.0273 - accuracy: 0.9899 - val_loss: 0.0267 - val_accuracy: 0.9915\n",
      "Validation score: 0.991516179856987\n",
      "train index: [     0      1      2 ... 148513 148514 148516]\n",
      "test index: [    12     16     26 ... 148499 148504 148515]\n",
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 253s 65ms/step - loss: 0.0272 - accuracy: 0.9904 - val_loss: 0.0253 - val_accuracy: 0.9916\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 641s 166ms/step - loss: 0.0261 - accuracy: 0.9908 - val_loss: 0.0260 - val_accuracy: 0.9909\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 253s 65ms/step - loss: 0.0256 - accuracy: 0.9907 - val_loss: 0.0264 - val_accuracy: 0.9912\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 255s 66ms/step - loss: 0.0252 - accuracy: 0.9912 - val_loss: 0.0254 - val_accuracy: 0.9905\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 251s 65ms/step - loss: 0.0239 - accuracy: 0.9911 - val_loss: 0.0486 - val_accuracy: 0.9849\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 797s 206ms/step - loss: 0.0238 - accuracy: 0.9914 - val_loss: 0.0274 - val_accuracy: 0.9902\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 415s 107ms/step - loss: 0.0229 - accuracy: 0.9916 - val_loss: 0.0328 - val_accuracy: 0.9903\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 252s 65ms/step - loss: 0.0232 - accuracy: 0.9912 - val_loss: 0.0279 - val_accuracy: 0.9902\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 249s 64ms/step - loss: 0.0224 - accuracy: 0.9915 - val_loss: 0.0545 - val_accuracy: 0.9841\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 760s 197ms/step - loss: 0.0216 - accuracy: 0.9916 - val_loss: 0.0245 - val_accuracy: 0.9918\n",
      "Validation score: 0.9918393730052922\n",
      "train index: [     0      1      2 ... 148514 148515 148516]\n",
      "test index: [    10     14     23 ... 148498 148505 148507]\n",
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 525s 136ms/step - loss: 0.0225 - accuracy: 0.9917 - val_loss: 0.0221 - val_accuracy: 0.9923\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 240s 62ms/step - loss: 0.0219 - accuracy: 0.9919 - val_loss: 0.0191 - val_accuracy: 0.9928\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 716s 185ms/step - loss: 0.0219 - accuracy: 0.9920 - val_loss: 0.0278 - val_accuracy: 0.9888\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 1721s 445ms/step - loss: 0.0211 - accuracy: 0.9920 - val_loss: 0.0191 - val_accuracy: 0.9928\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 1154s 299ms/step - loss: 0.0208 - accuracy: 0.9920 - val_loss: 0.0188 - val_accuracy: 0.9926\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 1117s 289ms/step - loss: 0.0201 - accuracy: 0.9925 - val_loss: 0.0210 - val_accuracy: 0.9915\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 722s 187ms/step - loss: 0.0210 - accuracy: 0.9920 - val_loss: 0.0204 - val_accuracy: 0.9928\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 249s 64ms/step - loss: 0.0199 - accuracy: 0.9926 - val_loss: 0.0207 - val_accuracy: 0.9917\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 248s 64ms/step - loss: 0.0199 - accuracy: 0.9922 - val_loss: 0.0228 - val_accuracy: 0.9915\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 1093s 283ms/step - loss: 0.0188 - accuracy: 0.9925 - val_loss: 0.0188 - val_accuracy: 0.9930\n",
      "Validation score: 0.9930109481678988\n",
      "train index: [     0      1      2 ... 148513 148515 148516]\n",
      "test index: [     4      6     20 ... 148509 148510 148514]\n",
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 255s 66ms/step - loss: 0.0197 - accuracy: 0.9924 - val_loss: 0.0172 - val_accuracy: 0.9936\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 256s 66ms/step - loss: 0.0189 - accuracy: 0.9927 - val_loss: 0.0174 - val_accuracy: 0.9931\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 249s 64ms/step - loss: 0.0192 - accuracy: 0.9925 - val_loss: 0.0207 - val_accuracy: 0.9924\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 145s 37ms/step - loss: 0.0190 - accuracy: 0.9929 - val_loss: 0.0188 - val_accuracy: 0.9926\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 242s 62ms/step - loss: 0.0181 - accuracy: 0.9927 - val_loss: 0.0180 - val_accuracy: 0.9927\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 241s 62ms/step - loss: 0.0175 - accuracy: 0.9934 - val_loss: 0.0208 - val_accuracy: 0.9921\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 240s 62ms/step - loss: 0.0180 - accuracy: 0.9930 - val_loss: 0.0193 - val_accuracy: 0.9930\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 244s 63ms/step - loss: 0.0179 - accuracy: 0.9932 - val_loss: 0.0204 - val_accuracy: 0.9926\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 240s 62ms/step - loss: 0.0170 - accuracy: 0.9934 - val_loss: 0.0226 - val_accuracy: 0.9920\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 246s 64ms/step - loss: 0.0175 - accuracy: 0.9932 - val_loss: 0.0185 - val_accuracy: 0.9928\n",
      "Validation score: 0.9927685533066699\n",
      "train index: [     1      2      4 ... 148514 148515 148516]\n",
      "test index: [     0      3      5 ... 148500 148501 148508]\n",
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 244s 63ms/step - loss: 0.0179 - accuracy: 0.9931 - val_loss: 0.0149 - val_accuracy: 0.9934\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 456s 118ms/step - loss: 0.0171 - accuracy: 0.9933 - val_loss: 0.0183 - val_accuracy: 0.9917\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 1415s 366ms/step - loss: 0.0173 - accuracy: 0.9930 - val_loss: 0.0177 - val_accuracy: 0.9928\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 242s 63ms/step - loss: 0.0165 - accuracy: 0.9936 - val_loss: 0.0178 - val_accuracy: 0.9931\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 243s 63ms/step - loss: 0.0174 - accuracy: 0.9934 - val_loss: 0.0181 - val_accuracy: 0.9920\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 242s 62ms/step - loss: 0.0172 - accuracy: 0.9934 - val_loss: 0.0178 - val_accuracy: 0.9932\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 242s 63ms/step - loss: 0.0166 - accuracy: 0.9935 - val_loss: 0.0185 - val_accuracy: 0.9936\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 242s 63ms/step - loss: 0.0169 - accuracy: 0.9936 - val_loss: 0.0181 - val_accuracy: 0.9920\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 245s 63ms/step - loss: 0.0164 - accuracy: 0.9936 - val_loss: 0.0211 - val_accuracy: 0.9926\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 240s 62ms/step - loss: 0.0162 - accuracy: 0.9934 - val_loss: 0.0191 - val_accuracy: 0.9931\n",
      "Validation score: 0.9930917464549751\n",
      "train index: [     0      1      3 ... 148513 148514 148515]\n",
      "test index: [     2      8     11 ... 148491 148512 148516]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "3868/3868 [==============================] - 221s 57ms/step - loss: 0.0170 - accuracy: 0.9934 - val_loss: 0.0165 - val_accuracy: 0.9936\n",
      "Epoch 2/10\n",
      "3868/3868 [==============================] - 1345s 348ms/step - loss: 0.0166 - accuracy: 0.9936 - val_loss: 0.0183 - val_accuracy: 0.9933\n",
      "Epoch 3/10\n",
      "3868/3868 [==============================] - 3130s 809ms/step - loss: 0.0159 - accuracy: 0.9941 - val_loss: 0.0189 - val_accuracy: 0.9928\n",
      "Epoch 4/10\n",
      "3868/3868 [==============================] - 2976s 770ms/step - loss: 0.0157 - accuracy: 0.9939 - val_loss: 0.0182 - val_accuracy: 0.9933\n",
      "Epoch 5/10\n",
      "3868/3868 [==============================] - 3681s 952ms/step - loss: 0.0154 - accuracy: 0.9938 - val_loss: 0.0201 - val_accuracy: 0.9929\n",
      "Epoch 6/10\n",
      "3868/3868 [==============================] - 2613s 676ms/step - loss: 0.0157 - accuracy: 0.9939 - val_loss: 0.0202 - val_accuracy: 0.9917\n",
      "Epoch 7/10\n",
      "3868/3868 [==============================] - 3047s 788ms/step - loss: 0.0158 - accuracy: 0.9938 - val_loss: 0.0172 - val_accuracy: 0.9933\n",
      "Epoch 8/10\n",
      "3868/3868 [==============================] - 3043s 787ms/step - loss: 0.0151 - accuracy: 0.9942 - val_loss: 0.0224 - val_accuracy: 0.9910\n",
      "Epoch 9/10\n",
      "3868/3868 [==============================] - 3044s 787ms/step - loss: 0.0151 - accuracy: 0.9939 - val_loss: 0.0187 - val_accuracy: 0.9931\n",
      "Epoch 10/10\n",
      "3868/3868 [==============================] - 3048s 788ms/step - loss: 0.0152 - accuracy: 0.9938 - val_loss: 0.0176 - val_accuracy: 0.9928\n",
      "Validation score: 0.9927682611506141\n"
     ]
    }
   ],
   "source": [
    "for train_index, test_index in kfold.split(combined_data_X,y_train):\n",
    "    train_X, test_X = combined_data_X.iloc[train_index], combined_data_X.iloc[test_index]\n",
    "    train_y, test_y = y_train.iloc[train_index], y_train.iloc[test_index]\n",
    "    \n",
    "    print(\"train index:\",train_index)\n",
    "    print(\"test index:\",test_index)\n",
    "    \n",
    "    x_columns_train = new_train_df.columns.drop('Class')\n",
    "    x_train_array = train_X[x_columns_train].values\n",
    "    x_train_1=np.reshape(x_train_array, (x_train_array.shape[0], x_train_array.shape[1], 1))\n",
    "    \n",
    "    dummies = pd.get_dummies(train_y) # Classification\n",
    "    outcomes = dummies.columns\n",
    "    num_classes = len(outcomes)\n",
    "    y_train_1 = dummies.values\n",
    "    \n",
    "    x_columns_test = new_train_df.columns.drop('Class')\n",
    "    x_test_array = test_X[x_columns_test].values\n",
    "    x_test_2=np.reshape(x_test_array, (x_test_array.shape[0], x_test_array.shape[1], 1))\n",
    "    \n",
    "    dummies_test = pd.get_dummies(test_y) # Classification\n",
    "    outcomes_test = dummies_test.columns\n",
    "    num_classes = len(outcomes_test)\n",
    "    y_test_2 = dummies_test.values\n",
    "    \n",
    "   \n",
    "    model.fit(x_train_1, y_train_1,validation_data=(x_test_2,y_test_2), epochs=10)\n",
    "    \n",
    "    pred = model.predict(x_test_2)\n",
    "    pred = np.argmax(pred,axis=1)\n",
    "    y_eval = np.argmax(y_test_2,axis=1)\n",
    "    score = metrics.accuracy_score(y_eval, pred)\n",
    "    oos_pred.append(score)\n",
    "    print(\"Validation score: {}\".format(score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
